import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import static java.awt.event.InputEvent.ALT_DOWN_MASK;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FileDialog;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.FilenameFilter;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;



public class TopGUI implements ActionListener{
	private JFrame window;
	private JMenuItem newGame, open, save, close, rules, about;
	
	public TopGUI() {
		window = new JFrame("Match Game");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		
		JMenu file = new JMenu("File");
		file.setMnemonic('F');
		
		JMenu help = new JMenu("Help");
		help.setMnemonic('H');
		
		newGame = new JMenuItem("New Game");
		newGame.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
		newGame.addActionListener(this);
		
		open = new JMenuItem("Open");
		open.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
		open.addActionListener(this);
		
		save = new JMenuItem("Save");
		save.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
		save.addActionListener(this);
		
		close = new JMenuItem("Close");
		close.setAccelerator(KeyStroke.getKeyStroke('C', CTRL_DOWN_MASK));
		close.addActionListener(this);
		
		rules = new JMenuItem("Rules");
		rules.setAccelerator(KeyStroke.getKeyStroke('R', CTRL_DOWN_MASK));
		rules.addActionListener(this);
		
		about = new JMenuItem("About");
		about.setAccelerator(KeyStroke.getKeyStroke('A', CTRL_DOWN_MASK));
		about.addActionListener(this);
		
		file.add(newGame);
		file.add(open);
		file.add(save);
		file.add(close);
		
		help.add(rules);
		help.add(about);
		
		menuBar.add(file);
		menuBar.add(help);
		
		
		window.add(menuBar);
		
		window.pack();
		window.setVisible(true);
	
	}
	public void actionPerformed(ActionEvent event) {
		if(event.getSource() == newGame) newGame();
		else if (event.getSource() == save) saveGame();
		else if (event.getSource() == open) loadGame();
		else if(event.getSource() == close) {
			window.setVisible(false);
			System.out.print("Close");
			System.exit(JFrame.EXIT_ON_CLOSE);
		}			
		else if(event.getSource() == about)	aboutFrame();
		else if(event.getSource() == rules)	ruleFrame();
	
	}
	public void newGame() {
	
	}
	public void saveGame() {
		FileDialog saveDialog = new FileDialog(window, "Save Game", FileDialog.SAVE);
		saveDialog.show();
		while(!(saveDialog.getFile()).contains(".xml")){
			saveDialog = new FileDialog(window, "Save Game",FileDialog.SAVE);
			saveDialog.show();
		}
		try {
			PrintWriter output = new PrintWriter(new File(saveDialog.getDirectory() + "" + saveDialog.getFile()));
			
			System.out.println(saveDialog.getDirectory() + "" + saveDialog.getFile());
		}
		
		catch(FileNotFoundException e){
		}
		
		
	}
	public void loadGame() {
		String s = "";
		
		FileDialog loadDialog = new FileDialog(window, "Load Game");
		loadDialog.show();
		while(!(loadDialog.getFile()).contains(".xml")){
			loadDialog = new FileDialog(window, "Load Game");
			loadDialog.show();
		}
		try {
			Scanner inFile = new Scanner(new File(loadDialog.getDirectory() + "" + loadDialog.getFile()));
			while(inFile.hasNext()){
				s += " " + inFile.next();
			}
			System.out.println(loadDialog.getDirectory() + "" + loadDialog.getFile());
		}
		catch(FileNotFoundException e){
		}
		
		

		
	}
	public void aboutFrame() {
		JFrame about = new JFrame("About");
		JLabel text = new JLabel(" This program was developed as a team project in CS 232.");
		about.add(text);
		about.pack();
		about.setVisible(true);
		
	}
	public void ruleFrame() {
		JFrame rules = new JFrame("Rules");
		JLabel text = new JLabel(" RULES RULES RULES RULES RULES RULES RULES RULES");
		rules.add(text);
		rules.pack();
		rules.setVisible(true);
	}
	public static void main(String[] args) {
		TopGUI window = new TopGUI();
		
	}
}